<?php
/**
 *	[附件守护者(tyler_attachment_download.{core})] (C)2014-2099 Powered by @Tyler.
 *	Version: v1.01
 *	Date: 2014-6-18 01:31
 */

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}
class plugin_tyler_attachment_download {
}
class plugin_tyler_attachment_download_forum extends plugin_tyler_attachment_download
{
	function attachment_predownload()
	{
		global $_G;
		require_once 'source/function/function_cache.php';
		@include_once 'data/sysdata/cache_tyler_attachment.php';
		$today = date('Ymd',TIMESTAMP);

		if(!isset($contents[$today]))
		{
			$contents[$today] = array();
		}
		@list($_GE['aid'], $_GE['k'], $_GE['t'], $_GE['uid'], $_GE['tableid']) = daddslashes(explode('|', base64_decode($_GET['aid'])));
		//print_r($_GE);exit;
		$attachmentInfo = C::t('forum_attachment')->fetch_all_by_id('aid',$_GE['aid']);
		$attachmentDesc = C::t('forum_attachment_n')->fetch($attachmentInfo[$_GE['aid']]['tableid'],$_GE['aid']); 
		#后台设定用户组对应下载数量
		if(substr($_G['cache']['plugin']['tyler_attachment_download']['groupDownNum'],-1) == '|')
			$_G['cache']['plugin']['tyler_attachment_download']['groupDownNum'] = substr($_G['cache']['plugin']['tyler_attachment_download']['groupDownNum'],0,-1);
		$_g = explode('|',$_G['cache']['plugin']['tyler_attachment_download']['groupDownNum']);
		$_gf = array();
		foreach($_g as $_gg)
		{
			$tmp[] = explode('=', trim($_gg));
		}
		foreach($tmp as $t)
		{
			$_gf[$t[0]] = $t[1];
		}
		#如果不属于指定组则不做任何处理,返回主程序
		if(!isset($_gf[$_G['groupid']]))
		{
			return;
		}
		$downloadNums = array_count_values($contents[$today]);
		if(intval($downloadNums[$_G['uid']]) >= $_gf[$_G['groupid']])
			showmessage($_G['cache']['plugin']['tyler_attachment_download']['warning']);

		$n = count($contents[$today]);
		#2为收费 1为所有资源 3免费资源
		if($_G['cache']['plugin']['tyler_attachment_download']['type'] == 2)
		{
			if($attachmentDesc['price']>0)
			{
				$contents[$today][]=$_G['uid'];
			}
		}
		elseif($_G['cache']['plugin']['tyler_attachment_download']['type'] == 1)
		{
				$contents[$today][]=$_G['uid'];
		}
		elseif($_G['cache']['plugin']['tyler_attachment_download']['type'] == 3)
		{
			if($attachmentDesc['price'] == 0)
			{
				$contents[$today][]=$_G['uid'];
			}
		}
		if(count($contents[$today]) > $n)
		{
			$cacheArray .= "\$contents=".arrayeval($contents,1).";\n";
			writetocache('tyler_attachment', $cacheArray); 
		}
	}

}

?>